package com.curry.android.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * SharedPreferences配置项管理类
 * 
 */
@Deprecated
public class ConfigHelper {

	/**
	 * 获取初始化对象
	 * 
	 * @param ctx
	 *            上下文
	 * @param PREF_NAME
	 *            取的文件名
	 * @return
	 */
	public static SharedPreferences getSharedPreferences(Context ctx,
			String PREF_NAME) {
		SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME, 0);
		return mSettings;

	}

	/**
	 * 获取初始化对象
	 * 
	 * @param ctx
	 * @param PREF_NAME
	 * @param mode
	 * @return
	 */
	public static SharedPreferences getSharedPreferences(Context ctx,
			String PREF_NAME, int mode) {
		try {
			SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME,
					mode);
			return mSettings;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 对文件进行编辑
	 * 
	 * @param ctx
	 * @param PREF_NAME
	 * @return
	 */
	public static Editor getEditor(Context ctx, String PREF_NAME) {
		SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME, 0);
		Editor editor = mSettings.edit();
		return editor;

	}

	/**
	 * 对文件进行编辑
	 * 
	 * @param ctx
	 * @param PREF_NAME
	 * @param mode
	 * @return
	 */
	public static Editor getEditor(Context ctx, String PREF_NAME, int mode) {
		SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME, mode);
		Editor editor = mSettings.edit();
		return editor;

	}

	/**
	 * 保存key到配制文件
	 * 
	 * @param ctx
	 * @param PREF_NAME
	 * @param key
	 * @param value
	 */
	public static void saveKey(Context ctx, String PREF_NAME, String key,
			String value) {
		SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME, 0);
		Editor mEditor = mSettings.edit();
		mEditor.putString(key, value);
		mEditor.commit();
	}

	/**
	 * 保存boolean类型的key到配置文件
	 * 
	 * @param ctx
	 * @param PREF_NAME
	 * @param key
	 * @param value
	 */
	public static void saveBooleanKey(Context ctx, String PREF_NAME,
			String key, boolean value) {
		SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME, 0);
		Editor mEditor = mSettings.edit();
		mEditor.putBoolean(key, value);
		mEditor.commit();
	}

	/**
	 * 保存int到 PREF_NAME文件中
	 * 
	 * @param ctx
	 * @param PREF_NAME
	 * @param key
	 * @param value
	 */
	public static void saveIntKey(Context ctx, String PREF_NAME, String key,
			int value) {
		SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME, 0);
		Editor mEditor = mSettings.edit();
		mEditor.putInt(key, value);
		mEditor.commit();
	}

	/**
	 * 从 PREF_NAME文件中取出int值
	 * 
	 * @param ctx
	 * @param PREF_NAME
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static int loadIntKey(Context ctx, String PREF_NAME, String key,
			int defValue) {
		SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME, 0);
		return mSettings.getInt(key, defValue);
	}

	/**
	 * 从配制文件取出key
	 * 
	 * @param ctx
	 * @param PREF_NAME
	 * @param key
	 * @return
	 */
	public static String loadKey(Context ctx, String PREF_NAME, String key) {
		SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME, 0);
		return mSettings.getString(key, "");
	}

	public static boolean loadBooleanKey(Context ctx, String PREF_NAME,
			String key) {
		SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME, 0);
		return mSettings.getBoolean(key, false);
	}

	/**
	 * 从配制文件删除key
	 * 
	 * @param ctx
	 * @param PREF_NAME
	 * @param key
	 */
	public static void removeKey(Context ctx, String PREF_NAME, String key) {
		SharedPreferences mSettings = ctx.getSharedPreferences(PREF_NAME, 0);
		Editor mEditor = mSettings.edit();
		mEditor.remove(key);
		mEditor.commit();
	}

	public static boolean commit(Editor mEditor) {
		return mEditor.commit();
	}

	/**
	 * 从配制文件删除key
	 * 
	 * @param key
	 * @param mEditor
	 */
	public static void removeKey(String key, Editor mEditor) {
		mEditor.remove(key);
		mEditor.commit();
	}

	/**
	 * 删除Editor数据
	 * 
	 * @param mEditor
	 */
	public static void clearkeys(Editor mEditor) {
		mEditor.clear();
		mEditor.commit();
	}

	/**
	 * 保存用户登录信息
	 * 
	 * @param context
	 * @param hashMap
	 */
	public static void SaveUserInfo(Context context,
			HashMap<String, Object> hashMap) {

		Editor editor = ConfigHelper.getEditor(context, "UserInfo",
				context.MODE_PRIVATE);
		String userID = hashMap.get("loginID").toString();
		String loginName = hashMap.get("loginName").toString();
		String type = hashMap.get("isValid").toString();
		String pmID = hashMap.get("pmID").toString();
		String nickname = hashMap.get("nickname").toString();
		String trueName = hashMap.get("trueName").toString();
		String phone = hashMap.get("phone").toString();

		editor.putString("loginID", userID);
		editor.putString("loginName", loginName);
		editor.putString("isValid", type);
		editor.putString("pmID", pmID);
		editor.putString("trueName", trueName);
		editor.putString("nickname", nickname);
		editor.putString("phone", phone);
		editor.commit();
	}

	/**
	 * 获取登录用户信息
	 * 
	 * @param context
	 * @return hashMap
	 */
	public static HashMap<String, Object> getUserInfo(Context context) {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		SharedPreferences preferences = ConfigHelper.getSharedPreferences(
				context, "UserInfo", context.MODE_PRIVATE);
		hashMap.put("loginID", preferences.getString("loginID", ""));
		hashMap.put("loginName", preferences.getString("loginName", ""));
		hashMap.put("isValid", preferences.getString("isValid", ""));
		hashMap.put("pmID", preferences.getString("pmID", ""));
		hashMap.put("trueName", preferences.getString("trueName", ""));
		hashMap.put("nickname", preferences.getString("nickname", ""));
		hashMap.put("phone", preferences.getString("phone", ""));

		return hashMap;
	}

	/**
	 * 保存信息
	 * 
	 *            ,hashmap,pre_name
	 * @return
	 */

	public static void savePreferences(Context ctx, Map<String, String> map,
			String PREF_NAME) {
		try {
			Editor editor = ConfigHelper.getEditor(ctx, PREF_NAME,
					ctx.MODE_PRIVATE);

			Iterator iter = map.entrySet().iterator();
			while (iter.hasNext()) {
				Entry entry = (Entry) iter.next();
				editor.putString(entry.getKey().toString(), entry.getValue()
						.toString());
			}
			editor.commit();
		} catch (Exception x) {
			return;
		}
	}
	/**
	 *   
	 * @param ctx
	 * @param PREF_NAME
	 * @return
	 */
	public static SharedPreferences getPreferences(Context ctx, String PREF_NAME) {

		SharedPreferences preferences = ConfigHelper.getSharedPreferences(ctx,
				PREF_NAME, ctx.MODE_PRIVATE);

		return preferences;
	}

	
}
